/**
 * 2009/12/04
 */
package com.googlecode.felicalib.sample.polling;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.googlecode.felicalib.sample.Felica;

/**
 * @author o.tanaka
 */
public final class FelicaPollingHost {
    /**
     * ロギングインスタンス.
     */
    private static final Log LOG = LogFactory.getLog(FelicaPollingHost.class);

    private static FelicaPollingHostThread thread = null;

    /**
     * @return idm
     */
    public static String getIDm() {
        if (FelicaPollingHost.thread != null) {
            FelicaPollingHost.thread.getIDm();
            final Polling polling = FelicaPollingHost.thread.getPolling();
            return polling.getIDm();
        }
        return null;
    }

    /**
     * @return pmm
     */
    public static String getPMm() {
        if (FelicaPollingHost.thread != null) {
            FelicaPollingHost.thread.getPMm();
            final Polling polling = FelicaPollingHost.thread.getPolling();
            return polling.getPMm();
        }
        return null;
    }

    /**
     * @param aFelica
     *            Felica
     * @param aSystemCode
     *            SystemCode
     * @param stts
     *            POLLING/NOT_POLLING
     */
    public static void polling(
            final Felica aFelica,
            final int aSystemCode,
            final int stts) {

        if (stts == 0) {
            if (FelicaPollingHost.thread == null) {
                FelicaPollingHost.thread =
                    new FelicaPollingHostThread(
                        aFelica,
                        aSystemCode,
                        new Polling());
            }
            FelicaPollingHost.thread.start();
            FelicaPollingHost.thread.setRunLevel(true);
        } else {
            if (FelicaPollingHost.thread.isAlive()) {
                FelicaPollingHost.thread.end();
                try {
                    FelicaPollingHost.thread.join();
                    FelicaPollingHost.thread = null;
                } catch (final InterruptedException e) {
                    FelicaPollingHost.LOG.debug("stop:", e);
                }
            }
        }
    }

    /**
     * @param serviceCode
     *            serviceCode
     * @param addr
     *            addr
     * @return String data
     */
    public static String read(
            final int serviceCode,
            final byte addr) {
        if (FelicaPollingHost.thread != null) {
            FelicaPollingHost.thread.getData(serviceCode, addr);
            final Polling polling = FelicaPollingHost.thread.getPolling();
            return polling.getData(serviceCode, addr);
        }
        return null;
    }

    /**
     * @param serviceCode
     *            serviceCode
     * @param addr
     *            addr
     * @param aData
     *            data
     * @return boolean
     */
    public static boolean write(
            final int serviceCode,
            final byte addr,
            final byte[] aData) {
        if (FelicaPollingHost.thread != null) {
            return FelicaPollingHost.thread.write(
                    serviceCode, addr, aData);
        }
        return false;
    }

    /**
     * @param aFelica
     *            Felica
     * @param aSystemCode
     *            SystemCode
     */
    private FelicaPollingHost() {
        super();
    }

}
